package com.shopping.adminServlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.shopping.bean.Goods;
import com.shopping.service.GoodsService;
import com.shopping.service.impl.GoodsServiceImpl;

/**
 * @author 216
 *后台更改或删除用户
 */
@WebServlet("/updateGoods.do")
public class AdminServletUpdateGoods extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		FileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("UTF-8");
		int goodsId = 0;
		String keyword = null;
		String gName = null; 
		int gPrice = 0;
		String gType = null;
		String gIntro = null;
		String gImage = null;
		String gIf = null;
		int busineId = 0;
		String fileName = null;
		if(upload.isMultipartContent(req)) {
			try {
					List<FileItem> items =upload.parseRequest(req);
					for(FileItem item:items) {
						if(item.isFormField()) {
							if("goodsId".equals(item.getFieldName())) {
								goodsId = Integer.parseInt(item.getString("UTF-8"));
							}
							if("gName".equals(item.getFieldName())) {
								gName = item.getString("UTF-8");
							}
							if("keyword".equals(item.getFieldName())) {
								keyword = item.getString("UTF-8");
							}
							if("gPrice".equals(item.getFieldName())) {
								gPrice = Integer.parseInt(item.getString("UTF-8"));
							}
							if("gType".equals(item.getFieldName())) {
								gType = item.getString("UTF-8");
							}
							if("gIntro".equals(item.getFieldName())) {
								gIntro = item.getString("UTF-8");
							}
							if("gIf".equals(item.getFieldName())) {
								gIf = item.getString("UTF-8");
							}
							if("busineId".equals(item.getFieldName())) {
								busineId = Integer.parseInt(item.getString("UTF-8"));
							}
						}else {
							if(!item.getContentType().contains("image")) {
								req.setAttribute("msg", "请选择图片文件");
								req.getRequestDispatcher("porduct.jsp").forward(req, resp);;
								return;
							}
							//文件大小限制
							if(item.getSize() >1024 * 1024) {
								req.setAttribute("msg", "文件过大，请选择小于1m的图片上传");
								req.getRequestDispatcher("porduct.jsp").forward(req, resp);;
								return;
							}
							String realPath = "c://userImgs";
							File folder = new File(realPath);
							if(!folder.exists()) {
								folder.mkdirs();
							}
							fileName = System.currentTimeMillis()+item.getName().substring(item.getName().lastIndexOf("."));
							File desFile = new File(realPath,fileName);
							try {
								item.write(desFile);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					GoodsService gs = new GoodsServiceImpl();
					gImage = "/shopping/goodsImage"+fileName;
					Goods oldGoods = new Goods();
					oldGoods.setGoodsId(goodsId);
					Goods newGoods = new Goods(gName, gPrice, gType, gIntro, gIf, busineId);
					newGoods.setgImage(gImage);
					if(keyword.equals("删除")) {
						gs.delGoodsById(goodsId);
						resp.sendRedirect("porduct.jsp");
						return;
					}
					gs.changeGoods(newGoods, oldGoods);
					
					resp.sendRedirect("porduct.jsp");
				}
			}catch(Exception e) {
				
			}
		}
		
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}

	
}
